package com.fr.chicken.pay.com.shb.pay.main;
import com.fr.chicken.pay.com.shb.pay.util.RSAUtils;
import net.sf.json.JSONObject;


import java.text.SimpleDateFormat;
import java.util.Date;

public class QuickPayment {

    /**
     * 快捷支付
     *
     * @throws Exception
     */

    public static String quick(JSONObject businessContext) throws Exception {
        JSONObject businessHead = buildBusinessHead();
        System.out.println("{\"businessContext\":" + businessContext + ",\"businessHead\":" + businessHead + "}");
        // 加密请求数据
        String reqEncrypt = RSAUtils.verifyAndEncryptionToString(businessContext, businessHead, PayConfig.MCH_PRIVATE_KEY, PayConfig.SHB_PUBLIC_KEY);
        JSONObject context = new JSONObject();
        context.put("context", reqEncrypt);
        System.out.println(context.toString());
        String url = PayConfig.HOST_URL + PayConfig.UNIFIED_PAYMENT_URL + reqEncrypt;
        System.out.println("========请求地址======");
        System.out.println(url);
        return url;
    }


    /**
     * 请求头
     *
     * @return
     */
    public static JSONObject buildBusinessHead() {
        JSONObject businessHead = new JSONObject();
        businessHead.put("charset", "00");//字符集 00 : utf-8
        businessHead.put("version", "V1.0.0");//版本号
        businessHead.put("method", "payment");//方法
        businessHead.put("merchantNumber", PayConfig.MERCHANT_NUMBER);//商户号
        businessHead.put("requestTime", getSdfTimes());//请求时间
        businessHead.put("signType", "");//签名类型
        businessHead.put("sign", "");//签名
        return businessHead;
    }

    /***
     * 请求内容
     * @return
     */
    public static JSONObject buildBusinessContextFormQuickPay(int shopId) {
        JSONObject businessContext = new JSONObject();
            businessContext.put("attach", String.valueOf(shopId));//附加信息 "attach": "",
            businessContext.put("channelMapped", "PAY_SYB");//支付通道标识(必填)    "channelMapped": "SAND_PAY",
            businessContext.put("commodityBody", "商品信息");//商品信息(必填)      "commodityBody": "商品信息",
            businessContext.put("commodityDetail", "商品详情");//商品详情(必填)    "commodityDetail": "商品详情",
            businessContext.put("commodityRemark", "商品备注");//商品备注   "commodityRemark": "商品备注",
            businessContext.put("currenciesType", "CNY");//币种：人民币(必填)   "currenciesType": "CNY",

            businessContext.put("defrayalType", "ALI_H5");//支付方式(必填)  "defrayalType": "ALI_NATIVE",
            businessContext.put("merchantOrderNumber", System.currentTimeMillis());//商户订单号(必填),每次交易唯一 "merchantOrderNumber": "dsorderid2018011516",
            businessContext.put("notifyUrl", "http://39.108.134.13:10015/api/notify/shb");//异步通知(必填)   "notifyUrl": "http://192.168.2.185:8080/api/notify/shb",
            businessContext.put("orderTime", getSdfTimes());//订单时间(必填)         "orderTime": "20180116144607",
            businessContext.put("remark", "备注");//备注        "remark": "备注",    "returnUrl": "http://www.baidu.com",
            businessContext.put("returnUrl", "http://www.baidu.com");//同步通知
            businessContext.put("subMerchantNumber", PayConfig.MERCHANT_NUMBER);//商户号(必填)


            businessContext.put("terminalId", "设备ID");//设备ID
            businessContext.put("terminalIP", "8.8.8.8");//设备IP(必填)
            businessContext.put("tradeAmount", "1");//交易金额分(必填)
            businessContext.put("tradeCheckCycle", "T1");//结算周期(必填)
        return businessContext;
    }



    public static String getSdfTimes() {
        return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    }

}
